package com.woniuxy01;

import java.sql.*;
import java.util.List;
import java.util.Scanner;

/**
 * @author 作者
 * @create 2025-09-23 16:44
 */
public class Test02 {
    public static void main(String[] args) throws Exception {
        String sql="select * from teacher where tid=?";
        Teacher teacher = JdbcUtil.queryOne(JdbcUtil.getConnection(), Teacher.class, sql,1);
        System.out.println(teacher);
    }

    private static void test08() throws SQLException, InstantiationException, IllegalAccessException, NoSuchFieldException, ClassNotFoundException {
        String sql="select * from stu where sid=?";
        List<Stu> stus = JdbcUtil.queryList(JdbcUtil.getConnection(), Stu.class, sql,1);
        System.out.println(stus);
    }

    private static void test07() throws ClassNotFoundException, SQLException {
        Scanner scanner = new Scanner(System.in);
        //JDBC四大核心参数
        String className="com.mysql.cj.jdbc.Driver";
        String url="jdbc:mysql://localhost:3306/db3";
        String user="root";
        String password="123";
        Class.forName(className);
        Connection connection = DriverManager.getConnection(url, user, password);
        System.out.println("输入要查询学生的学号");
        int sid=scanner.nextInt();
        String sql="select * from stu where sid=?";
        PreparedStatement preparedStatement=connection.prepareStatement(sql);
        preparedStatement.setInt(1,sid);
        ResultSet resultSet=preparedStatement.executeQuery();
        //MetaData:元数据：描述数据的数据，数据的底层信息
        ResultSetMetaData metaData = resultSet.getMetaData();
        //getColumnCount()获取总列数
        for (int i = 1; i <= metaData.getColumnCount(); i++) {
            //getColumnName()获取列名
            System.out.println(metaData.getColumnName(i));
        }
        System.out.println("-------------------------");

        if(resultSet.next()){
            System.out.println(resultSet.getInt(1));
            System.out.println(resultSet.getString("sname"));
            System.out.println(resultSet.getString("password"));
            System.out.println(resultSet.getObject(4));
        }
        resultSet.close();
        preparedStatement.close();
        connection.close();
    }

    private static void test06() throws SQLException, ClassNotFoundException {
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入学号");
        int sid=scanner.nextInt();
        System.out.println("输入姓名");
        String sname=scanner.next();
        System.out.println("输入密码");
        String pwd=scanner.next();
        System.out.println("输入年龄");
        int age=scanner.nextInt();
        String sql="insert into stu values(?,?,?,?)";
        JdbcUtil.update(JdbcUtil.getConnection(),sql,sid,sname,pwd,age);
//        JdbcUtil.update(JdbcUtil.getConnection(),sql,new Object[]{sid,sname,pwd,age});
    }

    private static void test05() throws ClassNotFoundException, SQLException {
        Scanner scanner=new Scanner(System.in);
        String className="com.mysql.cj.jdbc.Driver";
        String url="jdbc:mysql://localhost:3306/db3";
        String user="root";
        String password="123";
        Class.forName(className);
        Connection connection = DriverManager.getConnection(url, user, password);
        System.out.println("输入用户名");
        //nextLine()接受空格
        String sname=scanner.nextLine();
        System.out.println("输入密码");
        String pwd=scanner.nextLine();
        String sql="select count(*) from stu where sname=? and password=?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1,sname);
        preparedStatement.setString(2,pwd);
        ResultSet resultSet = preparedStatement.executeQuery();
        int count= 0;
        if (resultSet.next()) {
            count = resultSet.getInt(1);
        }
        if(count>0){
            System.out.println("登录成功");
        }else{
            System.out.println("登录失败");
        }
        resultSet.close();
        preparedStatement.close();
        connection.close();
    }

    private static void test04() throws ClassNotFoundException, SQLException {
        Scanner scanner = new Scanner(System.in);
        //JDBC四大核心参数
        String className="com.mysql.cj.jdbc.Driver";
        String url="jdbc:mysql://localhost:3306/db3";
        String user="root";
        String password="123";
        Class.forName(className);
        Connection connection = DriverManager.getConnection(url, user, password);
        System.out.println("输入要查询学生的学号");
        int sid=scanner.nextInt();
        String sql="select * from stu where sid=?";
        PreparedStatement preparedStatement=connection.prepareStatement(sql);
        preparedStatement.setInt(1,sid);
        ResultSet resultSet=preparedStatement.executeQuery();
        if(resultSet.next()){
            System.out.println(resultSet.getInt(1));
            System.out.println(resultSet.getString("sname"));
            System.out.println(resultSet.getString("password"));
            System.out.println(resultSet.getObject(4));
        }
        resultSet.close();
        preparedStatement.close();
        connection.close();
    }

    private static void test03() throws ClassNotFoundException, SQLException {
        Scanner scanner = new Scanner(System.in);
        //JDBC四大核心参数
        String className="com.mysql.cj.jdbc.Driver";
        String url="jdbc:mysql://localhost:3306/db3";
        String user="root";
        String password="123";
        Class.forName(className);
        Connection connection = DriverManager.getConnection(url, user, password);
        System.out.println("输入学号");
        int sid=scanner.nextInt();
        System.out.println("输入姓名");
        String sname=scanner.next();
        System.out.println("输入密码");
        String pwd=scanner.next();
        System.out.println("输入年龄");
        int age=scanner.nextInt();
        //sql语句的对应的值都是用?充当占位符
        String sql="insert into stu values(?,?,?,?)";
        //创建PreparedStatement对象，使用sql语句做参数
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        //给?添加具体的值setXxx(下标从1开始,值)
        preparedStatement.setInt(1,sid);
        preparedStatement.setString(2,sname);
        preparedStatement.setObject(3,pwd);
        preparedStatement.setObject(4,age);
        //执行操作
        preparedStatement.executeUpdate();
        //关闭资源
        preparedStatement.close();
        connection.close();
        System.out.println(sql);
    }

    private static void test01() throws ClassNotFoundException, SQLException {
        Scanner scanner=new Scanner(System.in);
        String className="com.mysql.cj.jdbc.Driver";
        String url="jdbc:mysql://localhost:3306/db3";
        String user="root";
        String password="123";
        Class.forName(className);
        Connection connection = DriverManager.getConnection(url, user, password);
        System.out.println("输入用户名");
        //nextLine()接受空格
        String sname=scanner.nextLine();
        System.out.println("输入密码");
        String pwd=scanner.nextLine();
        String sql="select count(*) from stu where sname='"+sname+"' and password='"+pwd+"'";
//        System.out.println(sql);
        //select count(*) from stu where sname='ererere' or 1=1 -- rwrwrwrwrwrwrwr
        ////select count(*) from stu where sname='ererere' ; drop database xxxx ; --
        Statement statement = connection.createStatement();
        ResultSet resultSet = statement.executeQuery(sql);
        int count= 0;
        if (resultSet.next()) {
            count = resultSet.getInt(1);
        }
        if(count>0){
            System.out.println("登录成功");
        }else{
            System.out.println("登录失败");
        }
        resultSet.close();
        statement.close();
        connection.close();
    }
}
